home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / DirectInput / DeviceView / didevimg.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  12.9 KB  |  331 lines

  1. //-----------------------------------------------------------------------------
  2. // File: DIDevImg.h
  3. //
  4. // Desc: Header for CDIDevImage class, which encapsulates methods for drawing
  5. //       device images, callout strings, and object highlights.
  6. //
  7. // This code is provided to aid developers in creating custom interfaces for
  8. // device configuration. The provided interface is flexible enough for most
  9. // situations, but is freely modifyable.
  10. //
  11. // The CDIDevImage class acts as the interface to the UI client, and the 
  12. // exposed public methods should be called by the client to set the drawing
  13. // options and perform renderings. The CDIDIObject class is used as a helper
  14. // class, and the exposed public methods should not be called by client code.
  15. //
  16. // Copyright( c ) Microsoft Corporation. All rights reserved.
  17. //-----------------------------------------------------------------------------
  18.  
  19. #ifndef __DIDEVIMG_H__
  20. #define __DIDEVIMG_H__
  21.  
  22. #ifndef DIRECTINPUT_VERSION
  23. #define DIRECTINPUT_VERSION 0x0800
  24. #endif
  25.  
  26. #include <windows.h>
  27. #include <dinput.h>
  28. #include <d3d9.h>
  29. #include <tchar.h>
  30.  
  31. // macros
  32. #ifndef SAFE_DELETE
  33. #define SAFE_DELETE(p)  { if(p) { delete (p);     (p)=NULL; } }
  34. #endif //SAFE_DELETE
  35.  
  36. #ifndef SAFE_DELETE_ARRAY
  37. #define SAFE_DELETE_ARRAY(p)  { if(p) { delete[] (p);     (p)=NULL; } }
  38. #endif //SAFE_DELETE_ARRAY
  39.  
  40. #ifndef SAFE_RELEASE
  41. #define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
  42. #endif //SAFE_RELEASE
  43.  
  44. // global constants
  45. #define DIDICONST_MAX_IMAGE_WIDTH                 1024
  46. #define DIDICONST_MAX_IMAGE_HEIGHT                1024
  47. #define DIDICONST_CUSTOM_VIEW_WIDTH                400
  48. #define DIDICONST_CUSTOM_VIEW_HEIGHT               300
  49.  
  50. // SetCalloutState flags
  51. #define DIDICOS_HIGHLIGHTED                 0x00000001
  52. #define DIDICOS_INVISIBLE                   0x00000002
  53. #define DIDICOS_TOOLTIP                     0x00000004
  54.  
  55. // SetOutputImageSize flags
  56. #define DIDISOIS_DEFAULT                    0x00000000
  57. #define DIDISOIS_RESIZE                     0x00000001
  58. #define DIDISOIS_MAINTAINASPECTUSINGWIDTH   0x00000002
  59. #define DIDISOIS_MAINTAINASPECTUSINGHEIGHT  0x00000004
  60.  
  61. // Possible render targets
  62. enum DIDIRENDERTARGET
  63. {
  64.     DIDIRT_SURFACE,
  65.     DIDIRT_DC
  66. };
  67.  
  68. // Custom HRESULTs
  69. #define DI_IMAGENOTFOUND     \
  70.     MAKE_HRESULT(0, FACILITY_WIN32, ERROR_FILE_NOT_FOUND)
  71.  
  72. // Forward references
  73. class CDIDIObject;
  74. typedef TCHAR MAXSTRING[MAX_PATH];
  75.  
  76.  
  77.  
  78.  
  79. //-----------------------------------------------------------------------------
  80. // Name: CDIDevImage
  81. // Desc: Handles device image drawing 
  82. //-----------------------------------------------------------------------------
  83. class CDIDevImage
  84. {
  85. public:
  86.     // __________________________
  87.     // Constructors / Destructors
  88.     CDIDevImage();
  89.     ~CDIDevImage();
  90.  
  91.     // ______________
  92.     // Public Methods
  93.     HRESULT Init( LPDIRECTINPUTDEVICE8 pDID );
  94.     HRESULT SetCalloutState( DWORD dwObjId, DWORD dwCalloutState );
  95.     HRESULT GetCalloutState( DWORD dwObjId, LPDWORD pdwCalloutState );
  96.     HRESULT SetCalloutColors( DWORD dwObjId, COLORREF crColorNormal, COLORREF crColorHigh );
  97.     HRESULT GetCalloutColors( DWORD dwObjId, LPCOLORREF pcrColorNormal, LPCOLORREF pcrColorHigh );
  98.     HRESULT SetCalloutText( DWORD dwObjId, LPCTSTR strText );
  99.     HRESULT GetCalloutText( DWORD dwObjId, LPTSTR strText, DWORD dwSize );
  100.     HRESULT GetObjFromPoint( POINT Pt, LPDWORD pdwObjId );
  101.     HRESULT SetActiveView( DWORD dwView );
  102.     HRESULT GetActiveView( LPDWORD pdwView, LPDWORD pdwNumViews = NULL );
  103.     HRESULT GetViewForObj( DWORD dwObjId, LPDWORD pdwView );
  104.     HRESULT SetOutputImageSize( DWORD dwWidth, DWORD dwHeight, DWORD dwFlags );
  105.     HRESULT SetFont( HFONT hFont );
  106.     HRESULT SetColors( D3DCOLOR Background, COLORREF crCalloutNormal, COLORREF crCalloutHigh );
  107.     HRESULT Render( LPDIRECT3DTEXTURE9 pTexture );
  108.     HRESULT RenderToDC( HDC hDC );
  109.  
  110.     friend BOOL CALLBACK EnumDeviceObjectsCB( LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef );
  111.  
  112. protected:
  113.     // _________________
  114.     // Protected Methods
  115.     VOID    CleanUp();
  116.     HRESULT LoadImageInfo( LPDIRECTINPUTDEVICE8 pDIDevice );
  117.     HRESULT RenderToTarget( LPVOID pvTarget, DIDIRENDERTARGET eTarget );
  118.     HRESULT RenderCustomToTarget( LPVOID pvTarget, DIDIRENDERTARGET eTarget );
  119.     HRESULT CreateCustomImageInfo( LPDIRECTINPUTDEVICE8 pDIDevice );
  120.     HRESULT LoadImages();
  121.     HRESULT BuildCustomUI();
  122.     HRESULT CreateScaledSurfaceCopy( LPDIRECT3DSURFACE9 pSurfaceSrc, DWORD dwWidthSrc, DWORD dwHeightSrc, 
  123.                                      FLOAT fxScale, FLOAT fyScale, LPDIRECT3DSURFACE9 *ppSurfaceDest );
  124.     VOID    CreateFont();
  125.     VOID    DestroyImages();
  126.     HRESULT GetCustomUISize( SIZE* pSize );
  127.  
  128.     HRESULT AddObject( DWORD dwID );
  129.     CDIDIObject* GetObject( DWORD dwID );
  130.  
  131.  
  132.     // _________________
  133.     // Protected Members
  134.     BOOL                     m_bInitialized;    // Init method has been successfully run
  135.     BOOL                     m_bCustomUI;       // ImageInfo not found. UI is built by program
  136.     BOOL                     m_bInvalidUI;      // The custom UI needs to be recreated
  137.     MAXSTRING*               m_atszImagePath;   // Array of MAXSTRING strings for background images
  138.     HBITMAP*                 m_ahImages;        // Array of pointers to background bitmaps
  139.  
  140.     DWORD                    m_dwActiveView;    // Index of the currently active view
  141.     DWORD                    m_dwNumViews;      // Total number of views for this devic3    
  142.     
  143.     D3DCOLOR                 m_BkColor;         // Background color for main device image
  144.     HFONT                    m_hFont;           // Font to be used when drawing all text
  145.     
  146.     CDIDIObject**            m_apObject;        // Array of pointers to CDIDIObject data objects
  147.     DWORD                    m_dwNumObjects;    // Total number of objects
  148.     
  149.     DWORD                    m_dwWidthPref;     // User-set preferred width
  150.     DWORD                    m_dwHeightPref;    // User-set preffered height
  151.     DWORD                    m_dwScaleMethod;   // Method to use when scaling to preferred size
  152.                     
  153.  
  154. };
  155.  
  156.  
  157.  
  158.  
  159. //-----------------------------------------------------------------------------
  160. // Name: DIDICallout
  161. // Desc: Encapsulates information a specific view callout
  162. //-----------------------------------------------------------------------------
  163. struct DIDICallout
  164. {
  165.     RECT  rcInit;
  166.     RECT  rcScaled;
  167.     POINT aptLineInit[5];
  168.     POINT aptLineScaled[5];
  169.     DWORD dwNumPoints;
  170.     DWORD dwTextAlign;
  171. };
  172.  
  173.  
  174.  
  175. //-----------------------------------------------------------------------------
  176. // Name: DIDIOverlay
  177. // Desc: Encapsulates information a specific view overlay
  178. //-----------------------------------------------------------------------------
  179. struct DIDIOverlay
  180. {
  181.     MAXSTRING strImagePath;
  182.     RECT     rcInit;
  183.     RECT     rcScaled;
  184.     HBITMAP  hImage;
  185. };
  186.  
  187.  
  188.  
  189.  
  190. //-----------------------------------------------------------------------------
  191. // Name: CDIDIObject
  192. // Desc: Encapsulates information about the device object (axis, button, POV)
  193. //-----------------------------------------------------------------------------
  194. class CDIDIObject
  195. {
  196. public:
  197.     // __________________________
  198.     // Constructors / Destructors
  199.     CDIDIObject( DWORD dwID, DWORD dwNumViews ); 
  200.     ~CDIDIObject();
  201.  
  202.  
  203.     // ______________
  204.     // Public Methods
  205.     VOID    SetOverlay( DWORD dwViewID, LPCTSTR tszImagePath, RECT rect );
  206.     VOID    SetCallout( DWORD dwViewID, DWORD dwNumPoints, POINT *aptLine, RECT rect, DWORD dwTextAlign );
  207.     VOID    SetCalloutState( DWORD dwCalloutState ) {m_dwState = dwCalloutState;}
  208.     DWORD   GetCalloutState() {return m_dwState;}
  209.     VOID    SetCalloutColors( COLORREF crColorNormal, COLORREF crColorHigh ) { m_crNormColor = crColorNormal; m_crHighColor = crColorHigh; }
  210.     VOID    GetCalloutColors( LPCOLORREF pcrColorNormal, LPCOLORREF pcrColorHigh ) { if( pcrColorNormal ) *pcrColorNormal = m_crNormColor; if( pcrColorHigh ) *pcrColorHigh = m_crHighColor; }
  211.     VOID    SetCalloutText( LPCTSTR strText );
  212.     VOID    GetCalloutText( LPTSTR strText, DWORD dwSize );
  213.     DWORD   GetID() { return m_dwID; }
  214.     VOID    GetName( LPTSTR strName, DWORD dwSize ) { _tcsncpy( strName, m_strName, dwSize ); }
  215.     VOID    SetName( LPCTSTR strName ) { _tcsncpy( m_strName, strName, MAX_PATH-4 ); }
  216.     VOID    DestroyImages();
  217.     HRESULT AllocateViews( DWORD dwNumViews );
  218.     VOID    ScaleView( DWORD dwViewID, FLOAT fxScale, FLOAT fyScale );
  219.  
  220.     DIDICallout* GetCallout( DWORD dwViewID ) { return &m_aCallout[dwViewID]; }
  221.     DIDIOverlay* GetOverlay( DWORD dwViewID ) { return &m_aOverlay[dwViewID]; }
  222.  
  223. private:
  224.     // _______________
  225.     // Private Members
  226.     DWORD         m_dwID;
  227.     MAXSTRING     m_strCallout;
  228.     MAXSTRING     m_strName;
  229.  
  230.     COLORREF      m_crNormColor;
  231.     COLORREF      m_crHighColor;
  232.  
  233.     DWORD         m_dwState;
  234.     DWORD         m_dwNumViews;
  235.  
  236.     DIDICallout  *m_aCallout;
  237.     DIDIOverlay  *m_aOverlay;
  238.  
  239. };
  240.  
  241. //-----------------------------------------------------------------------------
  242. // Rendering constants and inline functions
  243. //-----------------------------------------------------------------------------
  244. const DWORD ALPHA_MASK = D3DCOLOR_ARGB(255, 0, 0, 0);
  245. const DWORD RED_MASK   = D3DCOLOR_ARGB(0, 255, 0, 0);
  246. const DWORD GREEN_MASK = D3DCOLOR_ARGB(0, 0, 255, 0);
  247. const DWORD BLUE_MASK  = D3DCOLOR_ARGB(0, 0, 0, 255);
  248.  
  249.  
  250. //-----------------------------------------------------------------------------
  251. // Name: GetAlpha, GetRed, GetGreen, GetBlue
  252. // Desc: Extracts the specified color component
  253. //-----------------------------------------------------------------------------
  254. inline BYTE GetAlpha( D3DCOLOR Color ) { return (BYTE)( Color >> 24 ); }
  255. inline BYTE   GetRed( D3DCOLOR Color ) { return (BYTE)( Color >> 16 ); }
  256. inline BYTE GetGreen( D3DCOLOR Color ) { return (BYTE)( Color >> 8  ); }
  257. inline BYTE  GetBlue( D3DCOLOR Color ) { return (BYTE)( Color );       }
  258.  
  259.  
  260.  
  261.  
  262. //-----------------------------------------------------------------------------
  263. // Name: ColorFromCR
  264. // Desc: Returns a D3DCOLOR from the given COLORREF
  265. //-----------------------------------------------------------------------------
  266. inline D3DCOLOR ColorFromCR( COLORREF cr ) 
  267.     return GetRValue(cr) << 16 | GetGValue(cr) << 8 | GetBValue(cr); 
  268. }
  269.  
  270.  
  271.  
  272.  
  273. //-----------------------------------------------------------------------------
  274. // Name: CRFromColor
  275. // Desc: Returns a COLORREF from the given D3DCOLOR
  276. //-----------------------------------------------------------------------------
  277. inline COLORREF CRFromColor( D3DCOLOR color ) 
  278.     return GetBlue(color) << 16 | GetGreen(color) << 8 | GetRed(color); 
  279. }
  280.  
  281.  
  282.  
  283.  
  284. //-----------------------------------------------------------------------------
  285. // Name: ScaleRect
  286. // Desc: Convenience inline function for scaling a RECT structure
  287. //-----------------------------------------------------------------------------
  288. inline VOID ScaleRect( const RECT* pRectSrc, RECT* pRectDest, FLOAT fxScale, FLOAT fyScale )
  289. {
  290.     pRectDest->top    = (LONG) ( fyScale * pRectSrc->top + 0.5 );
  291.     pRectDest->bottom = (LONG) ( fyScale * pRectSrc->bottom + 0.5 );
  292.     pRectDest->left   = (LONG) ( fxScale * pRectSrc->left + 0.5);
  293.     pRectDest->right  = (LONG) ( fxScale * pRectSrc->right + 0.5);
  294. }
  295.  
  296.  
  297.  
  298.  
  299. //-----------------------------------------------------------------------------
  300. // Name: ScalePoint
  301. // Desc: Convenience inline function for scaling a POINT structure
  302. //-----------------------------------------------------------------------------
  303. inline VOID ScalePoint( const POINT* pPointSrc, POINT* pPointDest, FLOAT fxScale, FLOAT fyScale )
  304. {
  305.     pPointDest->x    = (LONG) ( fxScale * pPointSrc->x + 0.5 );
  306.     pPointDest->y    = (LONG) ( fyScale * pPointSrc->y + 0.5 );
  307. }
  308.  
  309.  
  310.  
  311.  
  312. //-----------------------------------------------------------------------------
  313. // Helper Functions
  314. // These external functions don't require direct access to the private member
  315. // variables, but are used at one or more points within the class methods.
  316. //-----------------------------------------------------------------------------
  317. HRESULT ApplyOverlay( HBITMAP hbmpDest, CONST RECT* prcDest, HBITMAP hbmpSrc );
  318. HRESULT ApplyAlphaChannel( HBITMAP hbmpDest, HBITMAP hbmpAlpha, BOOL bOpaque );
  319. HRESULT FillBackground( HBITMAP hbmpDest, D3DCOLOR Fill );
  320. HRESULT CreateDIBSectionFromSurface( LPDIRECT3DSURFACE9 pSurface, HBITMAP* phBitmap, SIZE* pSize = NULL ); 
  321. HRESULT RestoreRect( HBITMAP hbmpDest, CONST RECT* prcDest, LPBYTE pSrcPixels );
  322. HRESULT DrawTooltip( HDC hdcRender, HDC hdcAlpha, LPCTSTR strTooltip, RECT* prcBitmap, 
  323.                      RECT* prcTruncated, COLORREF crFore, COLORREF crBack, COLORREF crBorder );
  324. IDirect3DSurface9* GetCloneSurface( int iWidth, int iHeight );
  325.  
  326. BOOL CALLBACK EnumDeviceObjectsCB( LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef );
  327.  
  328.  
  329. #endif  //__DIDEVIMG_H__